<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\VillagePublish;
use App\Models\SystemRegion;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Support\Helper;

class VillagePublishController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VillagePublish(), function (Grid $grid) {
            $region_id = Admin::user()->region_id;
            if ($region_id) {
                $grid->model()->where('region_id', $region_id);
            }

            $grid->column('id')->sortable();
            $grid->column('pub_title');
//            $grid->column('pub_intro');
            $grid->column('list_img')->image('', 60, 60);

            $grid->column('region_id', '所属地区')->display(function ($region_id) {
                if ($region_id) {
                    $region = \App\Models\SystemRegion::find($region_id);
                    return $region ? $region->region_name : '';
                } else {
                    return '';
                }
            });

            $grid->column('is_top', '置顶')->switch()->sortable();
            $grid->column('weight');
            $grid->column('is_show', '是否展示')->switch()->sortable();
//            $grid->column('check_status', '审核状态')->select([0=>'待审核', 1=>'拒绝', 2=>'通过']);
            $grid->column('check_status', '审核状态')->display(function ($check_status) {
                return $check_status == 1 ? '未通过' : ($check_status == 2 ? '已通过' : '待审核'); // 审核状态，未审核0，未通过1，已通过2
            });
//            $grid->column('passed_at');


            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('is_show', '展示状态', [0 => '已隐藏', 1 => '已显示']);
                $selector->selectOne('check_status', '审核状态', [0 => '待审核', 1 => '未通过', 2 => '已通过']);
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VillagePublish(), function (Show $show) {
            $show->field('id');
            $show->field('pub_title');
            $show->field('pub_content');
            $show->field('pub_intro');
            $show->field('list_img');
            $show->field('region_id');
            $show->field('read_num');
            $show->field('zan_num');
            $show->field('share_num');
            $show->field('is_top');
            $show->field('weight');
            $show->field('user_id');
            $show->field('admin_id');
            $show->field('is_admin');
            $show->field('contact');
            $show->field('mobile');
            $show->field('check_status');
            $show->field('passed_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new VillagePublish(), function (Form $form) {
            $form->display('id');
            $form->text('pub_title')->required();
            $form->text('pub_intro');
            $region_id = Admin::user()->region_id;
            if ($region_id) {
                // 非总管理员
                $region = SystemRegion::find($region_id);
                $form->select('region_id', '所属地区')->options([$region_id => $region->region_name])->default($region_id, true)->required();
            } else {
                $region = \App\Admin\Repositories\SystemRegion::getList();
                $form->select('region_id', '所属地区')->options($region)->default(0, true)->required();
            }

            $form->hidden('list_img');
            $form->multipleImage('imgs')->move('village_publish/'.date('Y-m-d'))->saving(function ($paths) {
                    $paths = Helper::array($paths);
                    return json_encode($paths, true); // 最终转化为json保存到数据库
                })->customFormat(function ($paths) {
                    return Helper::array($paths); // 转为数组
                })->autoUpload();;

            $form->editor('pub_content')->required();
            $form->text('contact');
            $form->text('mobile');
            $form->radio('is_show', '是否展示')->options([1 => '是', 0 => '否'])->default(0);
            $form->radio('is_top', '置顶')->options([1 => '是', 0 => '否'])->default(0);
            $form->number('weight')->placeholder('权重越高文章越靠前');

            $form->number('read_num');
            $form->number('zan_num');
            $form->number('share_num');


//            $form->text('user_id');
            $form->hidden('admin_id')->value(Admin::user()->getKey());
            $form->hidden('user_id')->value(Admin::user()->user_id);
            $form->hidden('is_admin')->value(1);

            $adminRolesId = array_column(Admin::user()->roles->toArray(), 'id'); // 获得管理员角色

            // 公共内容管理员（品控管理） 有权限审核
            if (in_array(4, $adminRolesId)) {
                $form->radio('check_status', '审核')->options([2 => '通过', 1 => '驳回'])->default(0);
            } else {
                $form->hidden('check_status'); // 审核状态，未审核0，未通过1，已通过2
            }
        
            $form->display('created_at');
            $form->display('updated_at');
            $form->hidden('passed_at');


            $form->submitted(function ($form) {

                $form -> list_img = explode(',',$form -> imgs)[0];

                if ($form->model()->id) {
                    $publish = \App\Models\VillagePublish::find($form->model()->id);

                    if ($publish->check_status != 2 && $form->check_status == 2) {
                        $form->passed_at = date('Y-m-d H:i:s');
                    }
                }

            });

        });
    }
}
